﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;

namespace MountainORocks
{
    class Player
    {
        enum PlayerState { walk1, walk2, stand1, stand2, up };
        SpriteEffects[] side = { SpriteEffects.None, SpriteEffects.FlipHorizontally };
        int myside = 1;
        PlayerState playerState = PlayerState.stand1;
        public float forceFactor = 10000000f;
        bool isJumping = false;
        Game1 myGame;
        bool isDead = false;
        static Texture2D playerTexture, playerDeadTexture;
        int x = 0;
        int y = 0;
        bool needReset = true;


        public void setNotDead() { isDead = false; }
        public float powerMult = 1f;

        public Player(Game1 thisGame)
        {
            myGame = thisGame;
        }

        public static void loadContact(Texture2D playerTex, Texture2D playerDead)
        {
            playerTexture = playerTex;
            playerDeadTexture = playerDead;
        }

        public void resetDead()
        {
            if (isDead)
            {
                if (!needReset)
                {
                    isJumping = false;
                    myGame.gameState = MountainORocks.Game1.GameState.lose;
                    isDead = false;
                    myGame.charBody.CollisionCategories = Category.All;
                    return;
                }
                isDead = false;
                myGame.live--;
                isJumping = false;
                myGame.charBody.CollisionCategories = Category.All;
            }
            return;

        }


        public bool charBody_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            int check = 0;
            if (isDead)
                return false;
            /*
            if (fixtureA.Body == myGame.charBody)
            {
                if (fixtureB.Body == myGame.exitDoor)
                {
                    myGame.delay = 4.0f;
                    if (myGame.level < myGame.levelCount) { myGame.gameState = MountainORocks.Game1.GameState.win; }
                    else { myGame.gameState = MountainORocks.Game1.GameState.done; }

                }
                if (((ShapeM)(fixtureB.Body.UserData) == ShapeM.Crystal))
                {
                    myGame.playerMoney += 10;
                }
                Vector2 colNorm = contact.Manifold.LocalNormal;
                if (Math.Abs(colNorm.X) > Math.Abs(colNorm.Y))
                {
                    // X direction is dominant
                    if (colNorm.X > 0)
                    {
                        if ((myside == 1) && (fixtureA.Body.LinearVelocity.X > 0))
                        {
                            return true;
                        }
                        else
                            if (fixtureB.Body.AngularVelocity > 3)
                            {
                                isJumping = false;
                                if (myGame.live <= 0)
                                {
                                    isDead = true;
                                    needReset = false;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }
                                else
                                {

                                    isDead = true;
                                    needReset = true;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }

                            }

                        //direction = CollisionDirection.Right;
                    }
                    else
                    {
                        if ((myside == 0) && (fixtureA.Body.LinearVelocity.X > 0))
                        {
                            return true;
                        }
                        else
                            if (fixtureB.Body.AngularVelocity > 3)
                            {
                                isJumping = false;
                                if (myGame.live <= 0)
                                {
                                    isDead = true;
                                    needReset = false;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }
                                else
                                {

                                    isDead = true;
                                    needReset = true;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }

                            }
                        //direction = CollisionDirection.Left;
                    }
                }
                else
                {
                    // Y direction is dominant
                    if (colNorm.Y > 0)
                    {
                        isJumping = false;
                        // direction = CollisionDirection.Bottom;
                    }
                    else
                    {
                        if (fixtureB.Body.AngularVelocity > 3)
                        {
                            isJumping = false;
                            if (myGame.live <= 0)
                            {
                                isDead = true;
                                needReset = false;
                                myGame.charBody.CollisionCategories = Category.None;
                            }
                            else
                            {

                                isDead = true;
                                needReset = true;
                                myGame.charBody.CollisionCategories = Category.None;
                            }

                        }
                        //direction = CollisionDirection.Top;
                    }

                }

            }

            if (fixtureB.Body == myGame.charBody)
            {
                if (fixtureA.Body == myGame.exitDoor)
                {
                    myGame.delay = 4.0f;
                    if (myGame.level < myGame.levelCount) { myGame.gameState = MountainORocks.Game1.GameState.win; }
                    else { myGame.gameState = MountainORocks.Game1.GameState.done; }

                }
                if (((ShapeM)(fixtureA.Body.UserData) == ShapeM.Crystal))
                {
                    myGame.playerMoney += 10;
                }
                Vector2 colNorm = contact.Manifold.LocalNormal;
                if (Math.Abs(colNorm.X) > Math.Abs(colNorm.Y))
                {
                    // X direction is dominant
                    if (colNorm.X > 0)
                    {
                        if ((myside == 0) && (fixtureB.Body.LinearVelocity.X > 0))
                        {
                            return true;
                        }
                        else
                            if (fixtureA.Body.AngularVelocity > 3)
                            {
                                isJumping = false;
                                if (myGame.live <= 0)
                                {
                                    isDead = true;
                                    needReset = false;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }
                                else
                                {

                                    isDead = true;
                                    needReset = true;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }

                            }

                        //direction = CollisionDirection.Right;
                    }
                    else
                    {
                        if ((myside == 1) && (fixtureB.Body.LinearVelocity.X > 0))
                        {
                            return true;
                        }
                        else
                            if (fixtureA.Body.AngularVelocity > 3)
                            {
                                isJumping = false;
                                if (myGame.live <= 0)
                                {
                                    isDead = true;
                                    needReset = false;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }
                                else
                                {

                                    isDead = true;
                                    needReset = true;
                                    myGame.charBody.CollisionCategories = Category.None;
                                }

                            }
                        //direction = CollisionDirection.Left;
                    }
                }
                else
                {
                    // Y direction is dominant
                    if (colNorm.Y > 0)
                    {
                        if (fixtureA.Body.AngularVelocity > 3)
                        {
                            isJumping = false;
                            if (myGame.live <= 0)
                            {
                                isDead = true;
                                needReset = false;
                                myGame.charBody.CollisionCategories = Category.None;
                            }
                            else
                            {

                                isDead = true;
                                needReset = true;
                                myGame.charBody.CollisionCategories = Category.None;
                            }

                        }

                       
                        // direction = CollisionDirection.Bottom;
                    }
                    else
                    {
                        isJumping = false;
                        //direction = CollisionDirection.Top;
                    }

                }

            }


            */

            if (fixtureB.Body == myGame.charBody)
            {

                if (fixtureA.Body == myGame.exitDoor)
                {
                    myGame.delay = 4.0f;
                    if (myGame.level < myGame.levelCount) { myGame.gameState = MountainORocks.Game1.GameState.win; }
                    else { myGame.gameState = MountainORocks.Game1.GameState.done; }
                    check = 1;
                }

                if (((ShapeM)(fixtureA.Body.UserData)==ShapeM.Crystal))
                {
                    myGame.playerMoney += 10;
                    check = 1;
                }

                if (fixtureB.Body.LinearVelocity.Y > 1)
                {
                    isJumping = false;
                    check = 1;
                }

                if ((fixtureA.Body.Position.Y > fixtureB.Body.Position.Y + 31) || (fixtureA.Body.Position.X + 32 < fixtureB.Body.Position.X))
                {
                    isJumping = false;
                    check = 1;
                }
                if (fixtureB.Body.Position.Y > myGame.HEIGHT - 30)
                {
                    isJumping = false;
                    check = 1;
                }
                //if ((fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(50, -30))) && ((fixtureA.Body.LinearVelocity.X > 10) || (fixtureA.Body.LinearVelocity.Y > 10)) || (fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(2, -6))) && ((fixtureA.Body.LinearVelocity.X > 10) || (fixtureA.Body.LinearVelocity.Y > 10)))
                if (((fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(50, -30))) && ((fixtureA.Body.LinearVelocity.X > 50) || (fixtureA.Body.LinearVelocity.Y > 10))) || (fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(33, -2))) && ((fixtureA.Body.LinearVelocity.X > 50) || (fixtureA.Body.LinearVelocity.Y > 10)) || (fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(2, -6))) && ((fixtureA.Body.LinearVelocity.X > 50) || (fixtureA.Body.LinearVelocity.Y > 10)))
                    //if (check==0)
                {

                    if (myGame.live <= 0)
                    {
                        isDead = true;
                        needReset = false;
                        myGame.charBody.CollisionCategories = Category.None;
                    }
                    else
                    {

                        isDead = true;
                        needReset = true;
                        myGame.charBody.CollisionCategories = Category.None;
                    }
                }

                if (fixtureA == myGame.world.TestPoint(fixtureB.Body.Position + new Vector2(1, 33)))
                {
                    isJumping = false;

                }

            }
            if (fixtureA.Body == myGame.charBody)
            {

                if (fixtureB.Body == myGame.exitDoor)
                {
                    check = 1;
                    myGame.delay = 4.0f;
                    if (myGame.level < myGame.levelCount) { myGame.gameState = MountainORocks.Game1.GameState.win; }
                    else { myGame.gameState = MountainORocks.Game1.GameState.done; }

                }
                if (((ShapeM)(fixtureB.Body.UserData) == ShapeM.Crystal))
                {
                    myGame.playerMoney += 10;
                    check = 1;
                }

                //if (fixtureA.Body.LinearVelocity.Y > 1)
                  //  isJumping = false;
                if ((fixtureB.Body.Position.Y > fixtureA.Body.Position.Y + 31) || (fixtureB.Body.Position.X + 32 < fixtureA.Body.Position.X))
                {
                    isJumping = false;
                    check = 1;
                }
                if (fixtureA.Body.Position.Y > myGame.HEIGHT - 35)
                {
                    isJumping = false;
                    check = 1;
                }
                //if ((fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(50, -30))) && ((fixtureB.Body.LinearVelocity.X > 10) || (fixtureB.Body.LinearVelocity.Y > 10)) || (fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(2, -6))) && ((fixtureB.Body.LinearVelocity.X > 10) || (fixtureB.Body.LinearVelocity.Y > 10)))
                if (((fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(50, -30))) && ((fixtureB.Body.LinearVelocity.X > 50) || (fixtureB.Body.LinearVelocity.Y > 10))) || (fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(32, -2))) && ((fixtureB.Body.LinearVelocity.X > 50) || (fixtureB.Body.LinearVelocity.Y > 10)) || (fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(2, -6))) && ((fixtureB.Body.LinearVelocity.X > 50) || (fixtureB.Body.LinearVelocity.Y > 10)))
                    //if(check==0)
                {
                    if (myGame.live <= 0)
                    {
                        isDead = true;
                        needReset = false;
                        myGame.charBody.CollisionCategories = Category.None;
                    }
                    else
                    {

                        isDead = true;
                        needReset = true;
                        myGame.charBody.CollisionCategories = Category.None;
                    }

                }
                if (fixtureB == myGame.world.TestPoint(fixtureA.Body.Position + new Vector2(1, 33)))
                {
                    isJumping = false;

                }


            }

            //*/


            Console.WriteLine(contact.Manifold.Points[0].LocalPoint);




            return true;

        }


        public void Update(GameTime gameTime)
        {

            myGame.currentMouseState = Mouse.GetState();

            KeyboardState keyState = Keyboard.GetState();
            Vector2 force = Vector2.Zero;
            //if (keyState.IsKeyDown(Keys.Escape)) { this.Exit(); }
            if (isDead)
            {
                myGame.charBody.Position = new Vector2(myGame.charBody.Position.X, myGame.charBody.Position.Y - 3);
                if (myGame.charBody.Position.Y <= 0)
                {
                    if (needReset)
                    {
                        isDead = false;
                        myGame.live--;
                        isJumping = false;
                        myGame.buildWorld();
                        myGame.charBody.CollisionCategories = Category.All;
                    }
                    else
                    {
                        isJumping = false;
                        myGame.gameState = MountainORocks.Game1.GameState.lose;
                        isDead = false;
                        myGame.charBody.CollisionCategories = Category.All;
                    }
                }

                return;
            }
            
            if (keyState.IsKeyDown(Keys.Left) && !keyState.IsKeyDown(Keys.Up) && !isJumping)
            {
                myside = 0;
                if (playerState != PlayerState.walk2)
                    playerState = PlayerState.walk1;
                force.X -= forceFactor * (float)gameTime.ElapsedGameTime.TotalSeconds * powerMult;
            }

            if (keyState.IsKeyDown(Keys.Left)  && isJumping)
            {
                myside = 0;
                if (playerState != PlayerState.walk2)
                    playerState = PlayerState.walk1;
                force.X -= forceFactor / 5 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            if (keyState.IsKeyDown(Keys.Right) && !keyState.IsKeyDown(Keys.Up) && !isJumping)
            {
                myside = 1;
                if (playerState != PlayerState.walk2)
                    playerState = PlayerState.walk1;
                force.X += forceFactor * (float)gameTime.ElapsedGameTime.TotalSeconds * powerMult;
            }

            if (keyState.IsKeyDown(Keys.Right)  && isJumping)
            {
                myside = 1;
                if (playerState != PlayerState.walk2)
                    playerState = PlayerState.walk1;
                force.X += forceFactor /5  * (float)gameTime.ElapsedGameTime.TotalSeconds ;
            }

            if (keyState.IsKeyDown(Keys.Up) && !isJumping)
            {
                isJumping = true;
                if ((!keyState.IsKeyDown(Keys.Right)) && (!keyState.IsKeyDown(Keys.Left)))
                    playerState = PlayerState.up;
                force.Y -= forceFactor * 100 * (float)gameTime.ElapsedGameTime.TotalSeconds * (powerMult) ; 
            }
            if (keyState.IsKeyDown(Keys.Z)){ isJumping = false;}
          


            if ((playerState != PlayerState.stand2) && (force == Vector2.Zero) && (!isJumping))
                playerState = PlayerState.stand1;


           myGame.charBody.ApplyForce(force);

          


        }


        public void draw(SpriteBatch spriteBatch)
        {

            if ((myGame.timeBeforeChange < 0) && (playerState == PlayerState.stand1))
            {
                playerState = PlayerState.stand1;
                myGame.timeBeforeChange = myGame.timeBeforeChangeConst;
                x = 0;
                y = 0;
            }

            if ((myGame.timeBeforeChange < 0) && (playerState == PlayerState.stand2))
            {
                playerState = PlayerState.stand1;
                myGame.timeBeforeChange = myGame.timeBeforeChangeConst;
                x = 32;
                y = 0;
            }

            if ((myGame.timeBeforeChange < 0) && (playerState == PlayerState.walk1))
            {
                playerState = PlayerState.walk2;
                myGame.timeBeforeChange = myGame.timeBeforeChangeConst;
                x = 64;
                y = 0;
            }

            if ((myGame.timeBeforeChange < 0) && (playerState == PlayerState.walk2))
            {
                playerState = PlayerState.walk1;
                myGame.timeBeforeChange = myGame.timeBeforeChangeConst;
                x = 96;
                y = 0;
            }

            if (playerState == PlayerState.up)
            {
                x = 128;
                y = 0;
            }

            if (isDead)
            {
                spriteBatch.Draw(playerDeadTexture,
                    new Rectangle((int)myGame.charBody.Position.X, (int)myGame.charBody.Position.Y, 32, 32),
                    new Rectangle(0, 0, 32, 32),
                    Color.White);


            }
            else
            {

                spriteBatch.Draw(playerTexture,
                    new Rectangle((int)myGame.charBody.Position.X, (int)myGame.charBody.Position.Y, myGame.playerW, myGame.playerH),
                    new Rectangle(x, y, 19, 32),
                    Color.White,
                    myGame.charBody.Rotation,
                    new Vector2(4, 4), side[1 - myside], 0f);
            }



        }


    }
}
